package org.jradweb.tools.generator.utils;
/**
 * String Utils for Generator
 *
 * Created On 2007-12-02
 * @author <a href="mailto:wagzhi@gmail.com">Paul Wang</a>
 * @version $Id: StringUtils.java 148 2008-10-10 03:07:23Z wagzhi $
 */
public class StringUtils {
	/**
	 * convert 'xxx.xxxx' to 'xxx/xxxx'
	 * 
	 * @param packageName
	 * @return
	 */
	public static String packageToPath(String packageName){
		return packageName.replace('.', '/');
	}
	/**
	 * convert 'Xxxx' to 'xxxx'
	 * 
	 * @param name
	 * @return
	 */
	public static String lowerCaseFirst(String name){
		if(name==null||name.length()==0)
			return name;
		
		String first=name.substring(0,1);
		String lowFirst=first.toLowerCase();
		return lowFirst+name.substring(1,name.length());
	}
	
	/**
	 * convert 'xxxx' to 'Xxxx'
	 * 
	 * @param name
	 * @return
	 */
	public static String upperCaseFirst(String name){
		if(name==null||name.length()==0)
			return name;
		
		String first=name.substring(0,1);
		String upperFirst=first.toUpperCase();
		return upperFirst+name.substring(1,name.length());
	}
	/**
	 * convert 'AbcDef' to 'abc_def'
	 * @param name
	 * @return
	 */
	public static String lowerCaseName(String name){
		if(name==null||name.length()==0)
			return name;
		
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<name.length();i++){
			char c=name.charAt(i);
			String sc=""+c;
			
			if(sc.matches("[A-Z]{1}")){
				if(i==0)
					sc=sc.toLowerCase();
				else 
					sc="_"+sc.toLowerCase();
			}
			
			sb.append(sc);
		}
		
		return sb.toString();
	}
	
	public static void main(String[] args){
		String[]  names=new String[]{"L","Last","1last","LL"};
		for(String name:names){
			System.out.println(lowerCaseFirst(name));
		}
		System.out.println(lowerCaseFirst("__________________-"));
		String[]  names2=new String[]{"LastName","Hello","1234","abDe"};
		for(String name:names2){
			System.out.println(lowerCaseName(name));
		}
	}

}
